/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.google.code.p.narcissus.ui;

import java.io.File;

import com.google.code.p.narcissus.core.NarcissusException;
import com.google.code.p.narcissus.core.model.ReferenceMetaData;
import com.google.code.p.narcissus.core.reference.ReferenceMetaDataMapper;

/**
 * Acts as a storage of any information related to a specific reference, helping
 * using this information in the ui.
 *
 * @author jadoj
 */
public class TestReference {

    private File referenceFile;
    private ReferenceMetaData metaData;

    /**
     *
     * @param referenceFile File from which the metaData has t obe extracted
     * @throws NarcissusException if it is impossible to extract the metaData from the file
     */
    public TestReference(File referenceFile) throws NarcissusException {
        this.referenceFile = referenceFile;
        if(this.referenceFile == null){
            throw new NarcissusException(TestReference.class.getName() + ".referenceFileCannotBeNullEx");
        }

        // Load meta data if present.
        File metaDataFile = new File(this.referenceFile.getAbsolutePath().substring(0, this.referenceFile.getAbsolutePath().length() - 4) + ".xml");
        if (metaDataFile.exists() && metaDataFile.isFile()) {
            this.metaData = ReferenceMetaDataMapper.fromXml(metaDataFile);
        }
    }

    public File getReferenceFile() {
        return referenceFile;
    }

    public ReferenceMetaData getMetaData() {
        return metaData;
    }

    public void setMetaData(ReferenceMetaData metaData) {
        this.metaData = metaData;
    }

    public boolean metaDataExists(){
        return metaData != null;
    }

    /**
     * @return the name of the reference
     */
    @Override
    public String toString() {
        return referenceFile.getName().substring(0, referenceFile.getName().length() - 4);
    }
}